package weixinkeji.dev.dc.buy.order.so;

import java.util.List;
import java.util.Objects;
import sys.wx.m.db.SQL.mysql.SQLD;
import sys.wx.m.db.SQL.mysql.SQLI;
import sys.wx.m.db.SQL.mysql.SQLU;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.SQL.mysql.common.where.WhereConfig;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.m.db.dao.mysql.Dao2;
import weixinkeji.dev.dc.base.scheme.promotions.so.PromotionDaySO;
import weixinkeji.dev.dc.base.user.cache.FWYConfigControl;
import weixinkeji.dev.dc.buy.order.bean.FWYOrder;
import weixinkeji.dev.dc.buy.order.bean.FWYOrderB;
import weixinkeji.dev.dc.buy.order.sql.FWYSQL;
import weixinkeji.dev.dc.buy.order.xy.FWYOrderXY;

/**
 *
 * @author imyweb
 */
public class FWYOrderSO {

    /**
     * 添加一个食物。（可正可负），并统计总金额
     *
     * @param objb
     * @return boolean
     */
    public static boolean addOrderB(FWYOrderB objb) {
        //修改表头的金额
//        String Head = FWYSQL.SQL_UPDATE_ORDER_SUMPRICE.replace("?1", objb.getBuy_sum() + "").replace("?2", objb.getOrder_id());
        return Dao2.sql(SQLI.insert(objb),
                FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", objb.getOrder_id())
        );
    }

    /**
     * 修改清单中一个食物数量，并重新统计总金额
     *
     * @param objb
     * @return boolean
     */
    public static boolean updateOrderB(FWYOrderB objb) {
        //修改表头的金额
        String Head = FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", objb.getOrder_id());
        return Dao2.sql(SQLU.update(objb, "buy_number,buy_sum"), Head);
    }

    /**
     * 修改清单中一个食物数量,上台的数量，并重新统计总金额
     *
     * @param objb
     * @return boolean
     */
    public static boolean updateOrderB2(FWYOrderB objb) {
        //修改表头的金额
        String Head = FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", objb.getOrder_id());
        return Dao2.sql(SQLU.update(objb, "buy_number,buy_sum,orderb_lock_number,orderb_style"), Head);
    }

    /**
     * 删除订单-表体中，某一条记录。
     *
     * @param order_id 订单表头ID
     * @param orderb_id 订单表体ID
     * @return
     */
    public static boolean dellOrderB(String order_id, String orderb_id) {
        //修改表头的金额
        String Head = FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", order_id);
        //删除清单中一条记录，并更新订单头的数据
        return Dao2.sql(SQLD.dellOne(FWYOrderB.class, orderb_id), Head);
    }

    /**
     * 添加一个活动食物。（可正可负），并统计总金额，并减去活动购卖的数量。
     *
     * @param objb
     * @return boolean
     */
    public static boolean addOrderB_PromotionDay(FWYOrderB objb) {
        //修改表头的金额
        return Dao2.sql(SQLI.insert(objb),
                PromotionDaySO.updatePromotionDay(objb.getBuy_number() + "", objb.getSource_id()),
                FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", objb.getOrder_id()));
    }

    /**
     * 修改一个活动食物。（可正可负），并统计总金额，并减去活动购卖的数量。并修改活动数量
     *
     * @param objb
     * @param promotionDay_number
     * @return boolean
     */
    public static boolean updateOrderB_PromotionDay(FWYOrderB objb, Double promotionDay_number) {
        //修改表头的金额
        return Dao2.sql(SQLU.update(objb, "buy_number,buy_sum"),
                PromotionDaySO.updatePromotionDay(promotionDay_number + "", objb.getSource_id()),
                FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", objb.getOrder_id()));
    }

    /**
     * 修改一个活动食物的购卖量，并修改上台的数量。（可正可负），并统计总金额，并减去活动购卖的数量。并修改活动数量
     *
     * @param objb
     * @param promotionDay_number
     * @return boolean
     */
    public static boolean updateOrderB_PromotionDay2(FWYOrderB objb, Double promotionDay_number) {
        //修改表头的金额
        return Dao2.sql(SQLU.update(objb, "buy_number,buy_sum,orderb_lock_number"),
                PromotionDaySO.updatePromotionDay(promotionDay_number + "", objb.getSource_id()),
                FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", objb.getOrder_id()));
    }

    /**
     * 修改订单状态
     *
     * @param h_style 订单状态
     * @param id 指定订单
     * @return boolean
     */
    public static boolean updateOrder(String h_style, String id) {
        return Dao.executeUpdate(FWYSQL.SQL_UPDATE_ORDER_SORT.replace("?1", h_style).replace("?2", id));
    }

    /**
     * 根据源ID(正常菜谱ID，套餐菜谱ID，活动菜谱ID)，查询第一条记录。
     *
     * @param source_id
     * @param order_id
     * @return FWYOrderB
     */
    public static FWYOrderB selectOneFWYOrderB(String source_id, String order_id) {
        StringBuilder sb = new StringBuilder();
        sb.append("source_id='").append(source_id)
                .append("' AND order_id='").append(order_id).append("'");

        return Dao.selectOneByTJ(FWYOrderB.class, sb.toString());
    }

    /**
     * 用于服务员查询自己下的订单（或所有订单，看系统设置）的明细。
     *
     * @param order_id
     * @param account
     * @return
     */
    public static List<FWYOrderB> selectOneFWYOrderB_FWY(String order_id, String account) {
        Where x = new Where(new WhereConfig(""));
        x.put(Where.TJ_IGNORE_NOTHING, "", "order_id", "=", order_id);

        switch (FWYConfigControl.checkOK()) {

            case FWYConfigControl.FWY_SELECT_ALL: {
                return Dao.selectVast(FWYOrderB.class, x.getWhere());
            }

            case FWYConfigControl.FWY_SELECT_MY: {

                String sql_orderb = x.getWhere();
                x.put(Where.TJ_IGNORE_NOTHING, Where.AND, "user_account", "=", account);

                //查询此张订单是否是自己的订单，不是，直接返回null;
                if (Dao.selectCount(FWYOrder.class, x.getWhere()) == 0) {
                    return null;
                }
                //查询自己的单据
                return Dao.selectVast(FWYOrderB.class, sql_orderb);
            }
        }
        return null;
    }

    /**
     * 用于服务员查询自己下的订单（或所有订单，看系统设置）的明细。
     *
     * @param order_id
     * @param account
     * @return
     */
    public static List<FWYOrderB> dellOneFWYOrderB_FWY(String order_id, String account) {
        Where x = new Where(new WhereConfig(""));
        x.put(Where.TJ_IGNORE_NOTHING, "", "order_id", "=", order_id);

        switch (FWYConfigControl.checkOK()) {

            case FWYConfigControl.FWY_SELECT_ALL: {
                return Dao.selectVast(FWYOrderB.class, x.getWhere());
            }

            case FWYConfigControl.FWY_SELECT_MY: {

                String sql_orderb = x.getWhere();
                x.put(Where.TJ_IGNORE_NOTHING, Where.AND, "user_account", "=", account);

                //查询此张订单是否是自己的订单，不是，直接返回null;
                if (Dao.selectCount(FWYOrder.class, x.getWhere()) == 0) {
                    return null;
                }
                //查询自己的单据
                return Dao.selectVast(FWYOrderB.class, sql_orderb);
            }
        }
        return null;
    }

    /**
     * 返回null表示要添加，返回对象，表示只需要修改数量
     *
     * @param source_id
     * @param order_id
     * @return
     */
    public static FWYOrderB getFWYOrderB(String source_id, String order_id) {
        Where tj = new Where(new WhereConfig(""));
        tj.put(Where.TJ_IGNORE_NOTHING, "", "order_id", "=", order_id)
                .put(Where.TJ_IGNORE_NOTHING, Where.AND, "source_id", "=", source_id);

        List<FWYOrderB> blist = Dao.selectVast(FWYOrderB.class, tj.getWhere());
        FWYOrderB bobj;
        switch (blist.size()) {
            case 0:
                return null;
            case 1: {
                bobj = blist.get(0);
                return bobj.getOrderb_style() == FWYOrderXY.B_STYLE_1 ? bobj : null;
            }
            default: {
                for (FWYOrderB obj : blist) {
                    switch (obj.getOrderb_style()) {
                        case FWYOrderXY.B_STYLE_1: {
                            return obj;
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 服务员提交订单
     *
     * @param order_id
     * @return
     */
    public static boolean subimtOrder(String order_id) {
        return Dao2.sql(
                FWYSQL.SQL_DELL_ORDER_B_NUMBER_0.replace("?2", order_id),//清空订单里无效的清单——购卖量为0的
                FWYSQL.SQL_UPDATE_ORDER_SORT.replace("?1", FWYOrderXY.H_NOT_PAY_MONEY_ORDER).replace("?2", order_id),//变更订单为未付款状态
                FWYSQL.SQL_UPDTAE_ORDER_B_STYLE_OLD.replace("?1", "" + FWYOrderXY.B_STYLE_2).replace("?2", order_id).replace("?3", "" + FWYOrderXY.B_STYLE_1)//锁定订单里的清单
        );
    }

    public static boolean dellOrderB_Style1(String order_id) {
        return Dao2.sql(
                FWYSQL.SQL_DELL_ORDER_B_STYLE_1.replace("?2", order_id),//清空订单里无效的清单——购卖量为0的
                FWYSQL.SQL_UPDATE_ORDER_SUMPRICE_BY_SUMF.replace("?2", order_id)
        );
    }

    /**
     * 将指定订单里的锁定清单，送入厨房。
     *
     * @param order_id 订单ID
     * @return
     */
    public static boolean songruChufang(String order_id) {
        return Dao.executeUpdate(FWYSQL.SQL_UPDTAE_ORDER_B_STYLE_OLD
                .replace("?1", "" + FWYOrderXY.B_STYLE_3).replace("?2", order_id).replace("?3", "" + FWYOrderXY.B_STYLE_2));
    }

    public static boolean checkOrderIsLock(String order_id) {
        return Dao.selectOneByID(FWYOrder.class, order_id).getOrder_sort().equals(FWYOrderXY.H_PAY_MONEY_ORDER);
    }

    /**
     * 统计某(order_id)订单下，某食物(food_id)的总购买量(包括退回的数量)
     *
     * @param order_id
     * @param food_id
     * @return
     */
    public static Double getSum_BuyNumber(String order_id, String food_id) {
        Object x = Dao.selectCount2(
                FWYSQL.SQL_SELECT_SUM_ORDERB_BUYNUMBER.replace("?1", order_id).replace("?2", food_id)
        );
        return Double.parseDouble(x.toString());
    }

    public static boolean updateLockNumAndStyle(FWYOrderB b) {
        /**
         * 变更订单-表体的状态<p>
         * ?1:订单里，清单的状态<p>
         * ?2:上台数量 ?3:订单表体的 ID
         */
        return Objects.equals(b.getBuy_number(), b.getOrderb_lock_number())
                ? Dao.executeUpdate(
                        FWYSQL.SQL_UPDATE_ORDER_B_LOCKNUMBER_STYLE
                        .replace("?1", "" + FWYOrderXY.B_STYLE_4)
                        .replace("?2", "" + b.getOrderb_lock_number())
                        .replace("?3", b.getOrderb_id())
                )
                : Dao.executeUpdate(
                        FWYSQL.SQL_UPDATE_ORDER_B_LOCKNUMBER_STYLE
                        .replace("?1", "" + FWYOrderXY.B_STYLE_3)
                        .replace("?2", "" + b.getOrderb_lock_number())
                        .replace("?3", b.getOrderb_id())
                );
    }

}
